import { TextInput, List, Datagrid, TextField, DateField, Pagination ,SelectInput,BulkDeleteButton} from 'react-admin';
import { Button,useListContext ,useRefresh,useNotify,useUnselectAll} from 'react-admin';
import { dataProvider } from '../../dataProvider/DataProvider';
import { PushContactLogToWikiBtn } from './PushContactLogToWikiBtn'; 

const postFilters = [
    <TextInput label="通联活动" source="event_name" alwaysOn />,
    <TextInput label="OP呼号" source="op_Callsign" alwaysOn />,
    <SelectInput label="状态" source="status" alwaysOn choices={[
        { id: 'OPEN', name: '开启' },
        { id: 'CLOSED', name: '已结束' },
    ]}/>
];

const PostPagination = () => <Pagination rowsPerPageOptions={[5, 10, 25, 50, 100]} />;

const HandleDelete = (ids,notify,unselectAll,refresh)=>{
   
    dataProvider.deleteMany(import.meta.env.VITE_DELETE_CONTACT_EVENT_API_URI,ids,
        (ret)=>{
            notify("删除成功" + JSON.stringify(ret),{type:'info'});
            unselectAll();
            refresh();
        }
    )
   
}
const BulkActionBtns = ()=>{
    const { selectedIds } = useListContext();
    const refresh = useRefresh();
    const notify = useNotify();
    const unselectAll = useUnselectAll(import.meta.env.VITE_GET_CONTACT_EVENT_API_URI);

    
    return (
    <Button label="删除" onClick={()=>{
            HandleDelete(selectedIds,notify,unselectAll,refresh);
    
        }
    } />
    )
};


export const ContactEventPage = () => {

    return (
        <List filters={postFilters}  pagination={<PostPagination />} >
            <PushContactLogToWikiBtn label={"推送通联记录至WIKI"}/>
            <Datagrid bulkActionButtons = {<BulkActionBtns />}>
                <TextField source="id" label="序号" />
                <TextField source="eventName" label="通联活动" />
                <TextField source="status" label="状态" />
                <TextField source="opCallsign" label="OP" />
                <TextField source="recorder" label="记录员" />
                <TextField source="frequency" label="频率" />
                <TextField source="qth" label="QTH" />
                <TextField source="rig" label="设备" />
                <TextField source="ant" label="天线" />
                <TextField source="power" label="功率" />
                <TextField source="height" label="高度" />
                <TextField source="wikiTitle" label="WIKI Title" />
                <TextField source="pushBy" label="WIKI推送人" />
                <DateField source="pushDateTime" label="WIKI推送时间" />
                <DateField source="eventDateTime" label="时间" />
                <DateField source="createDate" label="创建时间" />
                <TextField source="createBy" label="创建人" />
            </Datagrid>
        </List>
    )
};